import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

class Record {
  constructor(record) {
    this.defId = record.goodsDefId;
    this.buyCount = record.buyCount;
    this.modelId = record.goodsModelId;
    this.price = record.goodsModelSalePrice;
    this.sourcePrice = record.goodsModelSourcePrice;

    let typeIds = record.goodsMajorType;
    if (SystemUtil.isEmpty(typeIds)) {
      typeIds = [];
    } else {
      typeIds = JSON.parse(typeIds);
    }
    this.typeIds = typeIds;
  }

  getBuyGoods() {
    return {
      goodsDefId: this.defId,
      goodsModelId: this.modelId,
      buyCount: this.buyCount
    };
  }
}

export default class BuyGoosRecord extends Base {
  constructor(recordList) {
    super();

    this.resetData(recordList);
  }

  resetData(recordList) {
    let records = [];
    let recordGroup = {};
    let recordDef = {};
    let recordObj = {};
    let totalPrice = 0,
      totalSourcePrice = 0;
    for (let i = 0; i < recordList.length; i++) {
      let record = recordList[i];
      let stockCount = record.stockCount;
      if (stockCount === 0) {
        continue;
      }

      record = new Record(record);
      records.push(record);
      recordGroup[record.modelId] = record;
      totalPrice += SystemUtil.roundValue(record.buyCount * record.price, 2);
      totalSourcePrice += SystemUtil.roundValue(record.buyCount * record.sourcePrice, 2);

      let defId = record.defId;
      let array = recordDef[defId];
      if (array === undefined) {
        array = [];
        recordDef[defId] = array;
      }
      array.push(record);

      let typeIds = record.typeIds;
      for (let i = 0; i < typeIds.length; i++) {
        const typeId = typeIds[i];
        let array = recordObj[typeId];
        if (array === undefined) {
          array = [];
          recordObj[typeId] = array;
        }
        array.push(record);
      }
    }

    this.recordList = records;
    this.recordGroup = recordGroup;
    this.recordDef = recordDef;
    this.recordObj = recordObj;
    this.totalPrice = SystemUtil.roundValue(totalPrice,2);
    this.totalSourcePrice = SystemUtil.roundValue(totalSourcePrice,2);
  }

  getRecordCountByTypeId(typeId) {
    let recordObj = this.recordObj;
    let data = recordObj[typeId];
    if (data === undefined) {
      return 0;
    }

    let count = 0;
    for (let i = 0; i < data.length; i++) {
      const obj = data[i];
      count += obj.buyCount;
    }
    return count;
  }

  getRecordCountByDefId(defId) {
    let recordDef = this.recordDef;
    let data = recordDef[defId];
    if (data === undefined) {
      return 0;
    }

    let count = 0;
    for (let i = 0; i < data.length; i++) {
      const obj = data[i];
      count += obj.buyCount;
    }
    return count;
  }

  getRecordByModelId(modelId) {
    let recordGroup = this.recordGroup;
    let record = recordGroup[modelId];
    if (record === undefined) {
      return null;
    }
    return record;
  }

  getTotalCount() {
    let recordList = this.recordList;
    let count = 0;
    for (let i = 0; i < recordList.length; i++) {
      const record = recordList[i];
      count += record.buyCount;
    }
    return count;
  }

  getGoodsList() {
    let recordList = this.recordList;
    let goodsList = [];
    for (let i = 0; i < recordList.length; i++) {
      const record = recordList[i];
      goodsList.push(record.getBuyGoods());
    }
    return goodsList;
  }
}
